/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple.data;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Loads RTE data formatted in xml.
 * 
 * @author Hideki Shima
 *
 */
public class RTEPairLoader {

  public static List<RTEPair> load(File f) {
    List<RTEPair> result = new ArrayList<RTEPair>();
    
    Document doc = null;
    try {
      DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = dbfactory.newDocumentBuilder();
      doc = builder.parse(new FileInputStream(f));
    } catch (Exception e) {
      e.printStackTrace();
    }
    Element root = doc.getDocumentElement();
    NodeList list = root.getElementsByTagName("pair");
    for (int i = 0; i < list.getLength(); i++) {
      Element element = (Element) list.item(i);
      String id = element.getAttribute("id");
      String label = element.getAttribute("value");
      if (label==null || label.length()==0) label = element.getAttribute("entailment"); 
      String task = element.getAttribute("task");
      String t1 = ((Element)element.getElementsByTagName("t").item(0)).getTextContent();
      String t2 = ((Element)element.getElementsByTagName("h").item(0)).getTextContent();
      
      result.add( new RTEPair(id, label, task, t1, t2) );
    }
    return result;
  }
}
